package com.tanhua.server.Listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.dubbo.api.mongo.VoicePublishApi;
import com.tanhua.dubbo.api.mongo.VoiceUserApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(
        topic = "tanhua_voice_topic",
        consumerGroup = "tanhua-voice-consumer")
public class VoiceMessageListener implements RocketMQListener<String> {

    @Reference
    private VoicePublishApi voicePublishApi;

    @Reference
    private VoiceUserApi voiceUserApi;

    @Override
    public void onMessage(String s) {

        // 获得用户id和语音路径
        Map message = JSON.parseObject(s, Map.class);
        Integer userId = (Integer)message.get("userId");
        String soundUrl = (String)message.get("soundUrl");
        // 保存 语音发布表
        ObjectId publishId = voicePublishApi.saveVoice(userId, soundUrl);
        // 保存 个人语音表
        voiceUserApi.saveVoice(userId,publishId);
        log.debug("************************保存语音完成,语音的路径:{}",soundUrl);
    }
}
